<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Db;
/**
 * 分类模型
 */
class Article extends Base{

	protected $auto = array('update_time');
	protected $insert = array('create_time');
	protected $type = array(
		'id'   => 'integer',
		'cover_id'   => 'integer',
	);
        public $category_id;
                
        function __construct() {
            parent::__construct();
            $this->setCategoryList();
        }

	public $keyList = array(
                array('name'=>'username', 'title'=>'用户名称', 'type'=>'readonly', 'help'=>'', 'option'=>''),
		array('name'=>'id', 'title'=>'ID', 'type'=>'hidden', 'help'=>'', 'option'=>''),
		array('name'=>'title', 'title'=>'文章标题', 'type'=>'text', 'help'=>'', 'option'=>''),		
		array('name'=>'category_id', 'title'=>'文章分类', 'type'=>'select', 'help'=>'', 'option'=>''),
		array('name'=>'view_num', 'title'=>'浏览数', 'type'=>'num', 'help'=>'', 'option'=>''),
		array('name'=>'status', 'title'=>'文章状态', 'type'=>'select', 'help'=>'','option'=>array('1'=>'审核通过','0'=>'未审核','2'=>'审核拒绝')),
//		array('name'=>'pic_list', 'title'=>'图片列表', 'type'=>'images', 'help'=>'', 'option'=>''),
                array('name'=>'keywords', 'title'=>'关键字', 'type'=>'text', 'help'=>'多个关键字请用逗号分隔', 'option'=>''),
                array('name'=>'content', 'title'=>'文章内容', 'type'=>'editor', 'help'=>'', 'option'=>''),
	);
        /**
         * 设置catlist
         */
        public function setCategoryList(){
            $this->keyList[3]['option']= self::getCategoryList();
        }
        /**
         * 获取分类列表
         */
        public static function getCategoryList(){
            $category_ids= \think\Db::name('category')->field('id,name')->select();
            $arr=[];
            foreach ($category_ids as $key=>$value){
                $arr[$value['id']]=$value['name'];
            }
            return $arr;
        }
       /**
        * 获取文章列表 
        */
        public static function getArticleList($where,$order){
            return Db::name('article a')
                    ->join('xy_category c','c.id = a.category_id')
                    ->join('xy_user u','u.id = a.user_id','left')
                    ->where($where)
                    ->order($order)
                    ->field('a.id,a.title,c.name,a.view_num,a.status,u.nickname,a.create_time,a.category_id')
                    ->paginate();
        }
        /**
         * 剔除文档中的图片地址
         */
        public static function getImgList($content){
            $pic_lsit=[];
            if(empty($content)){
                return '';
            }
            $ext='/\/upload.*?(?:jpg|png)/';
            preg_match_all($ext,$content, $pic_lsit);
            if(empty($pic_lsit)){
                return '';
            }
            $ids='';
            foreach($pic_lsit[0] as $k=>$v){
                $is_ext= Db::name('picture')->where(['url'=>$v])->value('id');
                if($is_ext){
                    $ids.=$is_ext.',';
                }else{
                    $insert=[
                       'url'=>$v,
                       'path'=>$v,
                       'md5'=>'',
                       'sha1'=>'',
                       'status'=>1,
                       'create_time'=> time()
                    ];
                    $id=Db::name('picture')->insertGetId($insert);
                    $ids.=$id.',';
                }
            }
            return trim($ids,',');
        }
}

